# -.- coding:utf-8 -.-
# __author__ = 'cuizc'
import numpy as np

import cv2
import tensorflow as tf


sess = tf.Session()
new_saver = tf.train.import_meta_graph('./model/model.ckpt-40.meta')
new_saver.restore(sess, "./model/model.ckpt-40")
graph = tf.get_default_graph()
x = graph.get_operation_by_name('x').outputs[0]
y = tf.get_collection("pred_network")[0]


def convert():
    for i in range(10):
        file = "./origin_data/dianshu/{0}.png".format(i)
        new_file = "./origin_data/dianshu1/{0}.png".format(i)
        img = cv2.imread(file, 0)
        ret, binary1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
        binary1 = cv2.resize(binary1, (36, 49), cv2.INTER_LINEAR)
        cv2.imwrite(new_file, binary1)


# convert()

def read_img(path):
    img = cv2.imread(path, 0)
    ret, binary1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)     # 轮次，总点数，本局点数
    binary1 = cv2.resize(binary1, (1920, 946), cv2.INTER_LINEAR)
    return binary1


def shibie(image):
    re_image = cv2.resize(image, (36, 49), cv2.INTER_LINEAR)
    aa = tf.argmax(sess.run(y, feed_dict={x: [re_image.reshape(-1)]}), 1)
    result = sess.run(aa)
    # print("预测值是:", result)
    return result[0]


# 4、分割字符
def fenge(image):
    white = []  # 记录每一列的白色像素总和
    black = []  # ..........黑色.......
    height = image.shape[0]
    width = image.shape[1]
    white_max = 0
    black_max = 0
    # 计算每一列的黑白色像素总和
    for i in range(width):
        s = 0  # 这一列白色总数
        t = 0  # 这一列黑色总数
        for j in range(height):
            if image[j][i] == 255:
                s += 1
            if image[j][i] == 0:
                t += 1
        white_max = max(white_max, s)
        black_max = max(black_max, t)
        white.append(s)
        black.append(t)

    arg = False  # False表示白底黑字；True表示黑底白字
    if black_max > white_max:
        arg = True

    n = 1
    num = 0
    while n < width - 2:
        n += 1
        if (white[n] if arg else black[n]) > (0.05 * white_max if arg else 0.05 * black_max):
            # 上面这些判断用来辨别是白底黑字还是黑底白字
            # 0.05这个参数请多调整，对应上面的0.95
            start = n
            end = start + 1
            for m in range(start + 1, width - 1):
                # 0.95这个参数请多调整，对应下面的0.05
                if (black[m] if arg else white[m]) > (0.95 * black_max if arg else 0.95 * white_max):
                    end = m
                    break
            n = end
            if end - start > 5:
                middle = (end + start) // 2
                start = middle - 12
                end = middle + 12
                # print("尺寸", height, end-start)
                cj = image[1:height, start:end]
                num = num * 10 + shibie(cj)
                # cv2.imshow(str(num), cj)
                # cv2.waitKey(0)
    print(num)
    return num


def get_num(image):
    img = cv2.imdecode(np.fromstring(image, np.uint8))
    ret, binary1 = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)  # 轮次，总点数，本局点数
    binary1 = cv2.resize(binary1, (1920, 946), cv2.INTER_LINEAR)
    # 轮次
    lunci = binary1[315:364, 459:534]
    # 总点数
    total_dianshu = binary1[515:554, 296:598]
    # 本次点数
    cur_dianshu = binary1[639:674, 296:598]
    lc = fenge(lunci)
    t_ds = fenge(total_dianshu)
    c_ds = fenge(cur_dianshu)
    return lc, t_ds, c_ds


# if __name__ == '__main__':
#     file = "./imgs/1549132297093.png"
#     img_obj = cv2.imread(file, 0)
#     print(get_num(img_obj))

